home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_349 / med / source / med200src.zoo / read8svx.c < prev   
C/C++ Source or Header  |  1990-04-08  |  7KB  |  221 lines

  1. /* MED - music editor by Teijo Kinnunen, 1990
  2.    This is the IFF 8SVX-loader of MED. I have done some modifications
  3.    to the original source from Fish #64 for MED. */
  4.  
  5. /** Read8SVX.c **********************************************************
  6.  * 
  7.  * Read a sound sample from an IFF file.  21Jan85 
  8.  * 
  9.  * By Steve Hayes, Electronic Arts. 
  10.  * This software is in the public domain. 
  11.  * 
  12.  * Linkage:
  13.  * LStartup.obj,Read8SVX.o,dunpack.o,iffr.o LIBRARY  LC.lib,Amiga.lib
  14.  *
  15.  ************************************************************************/
  16.  
  17. #include "exec/types.h" 
  18. #include "exec/exec.h" 
  19. #include "libraries/dos.h" 
  20. #include "iff/8svx.h" 
  21. #include "med.h"
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24.  
  25. /* Message strings for IFFP codes. */ 
  26. char MsgOkay[]        = { "(IFF_OKAY) No FORM 8SVX in the file." }; 
  27. char MsgEndMark[]     = { "(END_MARK) How did you get this message?" }; 
  28. char MsgDone[]        = { "(IFF_DONE) All done."}; 
  29. char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." }; 
  30. char MsgNot[]         = { "(NOT_IFF) Not an IFF file." }; 
  31. char MsgNoFile[]      = { "(NO_FILE) No such file found." }; 
  32. char MsgClientError[] = { "(CLIENT_ERROR) Read8SVX bug or insufficient RAM."}; 
  33. char MsgForm[]        = { "(BAD_FORM) A malformed FORM 8SVX." }; 
  34. char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM 8SVX." }; 
  35. char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." }; 
  36.  
  37. /* THESE MUST APPEAR IN RIGHT ORDER!! */ 
  38. char *IFFPMessages[-LAST_ERROR+1] = { 
  39.     /*IFF_OKAY*/  MsgOkay, 
  40.     /*END_MARK*/  MsgEndMark, 
  41.     /*IFF_DONE*/  MsgDone, 
  42.     /*DOS_ERROR*/ MsgDos, 
  43.     /*NOT_IFF*/   MsgNot, 
  44.     /*NO_FILE*/   MsgNoFile, 
  45.     /*CLIENT_ERROR*/ MsgClientError, 
  46.     /*BAD_FORM*/  MsgForm, 
  47.     /*SHORT_CHUNK*/  MsgShort, 
  48.     /*BAD_IFF*/   MsgBad 
  49.     }; 
  50.  
  51. typedef struct { 
  52.    ClientFrame clientFrame; 
  53.    UBYTE foundVHDR; 
  54.    UBYTE pad1; 
  55.    Voice8Header sampHdr; 
  56.    } SVXFrame; 
  57.  
  58.  
  59. /* NOTE: For a simple version of this program, set Fancy to 0. 
  60.  * That'll compile a program that skips all LISTs and PROPs in the input 
  61.  * file. It will look in CATs for FORMs 8SVX. That's suitable for most uses. 
  62.  * 
  63.  * For a fancy version that handles LISTs and PROPs, set Fancy to 1.
  64.  */
  65.  
  66. #define Fancy  0 
  67.  
  68. BYTE *buf; 
  69. int szBuf; 
  70.  
  71. /** ReadBODY() ***********************************************************
  72.  * 
  73.  * Read a BODY into RAM.  
  74.  * 
  75.  *************************************************************************/
  76. IFFP ReadBODY(context)  GroupContext *context;  { 
  77.     IFFP iffp; 
  78.  
  79.     szBuf  = ChunkMoreBytes(context); 
  80.     buf  = (BYTE *)AllocMem(sizeof(struct Soitin) + szBuf, MEMF_CHIP); 
  81.     if (buf == NULL) 
  82.         iffp = CLIENT_ERROR; 
  83.     else 
  84.         iffp = IFFReadBytes(context, (BYTE *)buf + sizeof(struct Soitin),
  85.         szBuf); 
  86.     CheckIFFP(); 
  87.     } 
  88.  
  89. /** GetFo8SVX() **********************************************************
  90.  * 
  91.  * Called via ReadSample to handle every FORM encountered in an IFF file. 
  92.  * Reads FORMs 8SVX and skips all others. 
  93.  * Inside a FORM 8SVX, it reads BODY. It complains if it 
  94.  * doesn't find an VHDR before the BODY. 
  95.  * 
  96.  * [TBD] We could read and print out any NAME and "(c) " chunks. 
  97.  * 
  98.  *************************************************************************/
  99. SVXFrame smusFrame;          /* only used for non-clientFrame fields.*/ 
  100. IFFP GetFo8SVX(parent)  GroupContext *parent;  { 
  101.    /*compilerBug register*/ IFFP iffp; 
  102.    GroupContext formContext; 
  103.  
  104.    if (parent->subtype != ID_8SVX) 
  105.       return(IFF_OKAY); /* just continue scaning the file */ 
  106.  
  107.    smusFrame = *(SVXFrame *)parent->clientFrame; 
  108.    iffp = OpenRGroup(parent, &formContext); 
  109.    CheckIFFP(); 
  110.  
  111.    do switch (iffp = GetFChunkHdr(&formContext)) { 
  112.       case ID_VHDR: { 
  113.         smusFrame.foundVHDR = TRUE; 
  114.         iffp = GetVHDR(&formContext, &smusFrame.sampHdr); 
  115.         break; } 
  116.       case ID_BODY: { 
  117.         if (!smusFrame.foundVHDR) 
  118.             iffp = BAD_FORM;            /* Need an VHDR chunk first! */ 
  119.         else iffp = ReadBODY(&formContext); 
  120.         break; } 
  121.       case END_MARK: { 
  122.         if (!smusFrame.foundVHDR) 
  123.             iffp = BAD_FORM; 
  124.         else 
  125.             iffp = IFF_DONE; 
  126.         break; } 
  127.       } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a 
  128.                                 * subroutine returned IFF_OKAY (no errors).*/ 
  129.  
  130.     if (iffp != IFF_DONE)  return(iffp); 
  131.  
  132.     /* If we get this far, there were no errors. */ 
  133.     CloseRGroup(&formContext); 
  134.     return(iffp); 
  135.     } 
  136.  
  137. /** Notes on extending GetFo8SVX ****************************************
  138.  * 
  139.  * To read more kinds of chunks, just add clauses to the switch statement. 
  140.  * To read more kinds of property chunks (like NAME) add clauses to 
  141.  * the switch statement in GetPr8SVX, too. 
  142.  * 
  143.  ************************************************************************/
  144.  
  145. /** GetPr8SVX() *********************************************************
  146.  * 
  147.  * Called via ReadSample to handle every PROP encountered in an IFF file. 
  148.  * Reads PROPs 8SVX and skips all others. 
  149.  * 
  150.  *************************************************************************/
  151. #if Fancy 
  152. IFFP GetPr8SVX(parent)  GroupContext *parent;  { 
  153.    /*compilerBug register*/ IFFP iffp; 
  154.    GroupContext propContext; 
  155.    SVXFrame *svxFrame = (SVXFrame *)parent->clientFrame; /* Subclass */ 
  156.  
  157.    if (parent->subtype != ID_8SVX) 
  158.       return(IFF_OKAY); /* just continue scaning the file */ 
  159.  
  160.    iffp = OpenRGroup(parent, &propContext); 
  161.    CheckIFFP(); 
  162.  
  163.    do switch (iffp = GetPChunkHdr(&propContext)) { 
  164.       case ID_VHDR: { 
  165.         svxFrame->foundVHDR = TRUE; 
  166.         iffp = GetVHDR(&propContext, &svxFrame->sampHdr); 
  167.         break; } 
  168.       } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a 
  169.                               * subroutine returned IFF_OKAY (no errors).*/ 
  170.  
  171.    CloseRGroup(&propContext); 
  172.    return(iffp == END_MARK ? IFF_OKAY : iffp); 
  173.    } 
  174. #endif 
  175.  
  176. /** GetLi8SVX() **********************************************************
  177.  * 
  178.  * Called via ReadSample to handle every LIST encountered in an IFF file. 
  179.  * 
  180.  *************************************************************************/
  181. #if Fancy 
  182. IFFP GetLi8SVX(parent)  GroupContext *parent;  { 
  183.     SVXFrame newFrame;  /* allocate a new Frame */ 
  184.  
  185.     newFrame = *(SVXFrame *)parent->clientFrame;  /* copy parent frame */ 
  186.  
  187.     return( ReadIList(parent, (ClientFrame *)&newFrame) ); 
  188.     } 
  189. #endif 
  190.  
  191. /** ReadSample() **********************************************************
  192.  * 
  193.  * Read IFF 8SVX, given a file handle open for reading.
  194.  * 
  195.  *************************************************************************/
  196. SVXFrame sFrame;     /* Top level "client frame".*/ 
  197. IFFP ReadSample(file)  LONG file;  { 
  198.    IFFP iffp = IFF_OKAY; 
  199.  
  200. #if Fancy 
  201.    sFrame.clientFrame.getList = GetLi8SVX; 
  202.    sFrame.clientFrame.getProp = GetPr8SVX; 
  203. #else 
  204.    sFrame.clientFrame.getList = SkipGroup; 
  205.    sFrame.clientFrame.getProp = SkipGroup; 
  206. #endif 
  207.    sFrame.clientFrame.getForm = GetFo8SVX; 
  208.    sFrame.clientFrame.getCat  = ReadICat ; 
  209.  
  210.    /* Initialize the top-level client frame's property settings to the 
  211.     * program-wide defaults. This example just records that we haven't read 
  212.     * any VHDR properties yet. 
  213.     * If you want to read another property, init it's fields in sFrame. */ 
  214.    sFrame.foundVHDR = FALSE; 
  215.    sFrame.pad1      = 0; 
  216.  
  217.    iffp = ReadIFF(file, (ClientFrame *)&sFrame); 
  218.  
  219.    return(iffp); 
  220.    } 
  221.